<!-- careLabel表单弹窗 -->
<form id="bedPatientForm" lay-filter="bedPatientForm" class="layui-form model-form">
    <div class="layui-input-block bedPatientForm-row1" style="left:-30px; top:-15px; font-size:22px;font-weight:500;">
    </div>
    <div class="layui-form-item" pane="">
        <label class="layui-form-label">护理标签：</label>
        <div class="careLabelList layui-input-block">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">护士姓名：</label>
        <div class="layui-input-inline">
            <select name="nurseCode" lay-filter="bedPatientForm-nurseCode" data-codeKey="code" data-textKey="name">
                <option value="" data-default=""></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">过敏类型：</label>
        <div class="layui-input-block">
            <input type="text" name="allergy" class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">入院诊断：</label>
        <div class="layui-input-block">
            <input type="text" name="diagnose" class="layui-input" />
        </div>
    </div>
    <div class="layui-form-item text-center">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="bedPatientForm-submit" type="button" lay-submit>保存</button>
        <button class="layui-btn" lay-filter="bedPatientForm-submit-close" type="button" lay-submit>保存&关闭</button>
    </div>
    <div id="test1"></div>

</form>

<script>
    layui.use(['form', 'admin','http','tools'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var admin = layui.admin;
        var http = layui.http;
        var tools = layui.tools;

        var viewId = "#bedPatientForm";
        var view = $(viewId);

        var $row1 = view.find(".bedPatientForm-row1");
        var $careLabelList = view.find(".careLabelList");
        var $nurseCode = view.find("select[name='nurseCode']");
        var allergy = view.find("input[name='allergy']");
        var diagnose = view.find("input[name='diagnose']");

        // 回显bedPatient的数据
        var rowData = tools.param.rowData;
        var deptCode = tools.param.deptCode;

        var fnRender = function (formData) {
            var nurseList = formData.nurseList;
            var careLabelList = formData.careLabelList;

            tools.select.options($nurseCode,nurseList);

            form.val('bedPatientForm', rowData);

            $row1.text('姓名: ' + (rowData.name||'')
                + '，床号: ' + (rowData.bedName2 || rowData.bedName ||'')
                + '，住院号: ' + (rowData.code||'')
                + '，' + (rowData.nurseLevelName||'')
            );

            //生成所有护理标签(隐藏未选择的标签)
            var showCareLabel = function () {
                var s = "";
                for(var i=0;i<careLabelList.length;i++){
                    var item = careLabelList[i];
                    s = s + '<input type="checkbox" name="careLabelIds" title="'+(item.labelName||'')+'" value="'+(item.id||'')+'" lay-skin="primary"';
                    if(item.flag){
                        s=s+' checked ';
                    }
                    s = s
                        +'style="background-color:'+item.bgColor+';'
                        +'color:'+item.fontColor+';'
                        +'">';
                }
                $careLabelList.html(s);

            };
            showCareLabel();

        };

        http.get('patient/getFormData',{patientId:rowData.id,deptCode:deptCode},function(res){
            if(res.ok){
                fnRender(res.data);
                form.render(null,'bedPatientForm');
                // $careLabelList.find("input[type='checkbox']").each(function(){
                //     $(this).next("div").find("span").attr("style",$(this).attr("style"));
                // });
            }
        });

        var fnGetCareLabelCheckedIds = function () {
            var s = [];
            $careLabelList.find("input[type='checkbox']").each(function(){
               if($(this).is(':checked')){
                    s.push(this.value);
                }
            });
            return s;
        };

        var fnSave = function(data,toClose){
            var fieldData = data.field;
            fieldData.careLabelIds = fnGetCareLabelCheckedIds();
            fieldData.id = rowData.id;
            http.post('patient/saveFormData', fieldData, function (res) {
                if (res.ok) {
                    $.extend(rowData,data.field);
                    http.okMsg(res.msg);
                    if(toClose) {
                        admin.closeDialog('#bedPatientForm');
                    }
                }
            });
        };
        // 表单提交事件
        form.on('submit(bedPatientForm-submit)', function (data) {
            fnSave(data,false);
            return false;
        });
        form.on('submit(bedPatientForm-submit-close)', function (data) {
            fnSave(data,true);
            return false;
        });
    });
</script>